﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ClassLibrary.Data;
using System.Collections.Generic;
using ClassLibrary.Entity;
using ClassLibrary.DAO;

public partial class ucControl_ucXacNhanThongTin : System.Web.UI.UserControl
{
    ItemsDAO itemDAO = new ItemsDAO(Global.CONNECTION_STRING);

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
         
            string type = Session["type"].ToString();
            if (type == "")
            {
                Response.Redirect("~/Default.aspx");
            }
            if (Session[Global.SESSION_LOGGED_USER] == null || Session[Global.SESSION_CART] == null)
            {
                Response.Redirect("~/Default.aspx");
            }
            else
            {
                lb_email.Text = ((TblUserAccount)Session[Global.SESSION_LOGGED_USER]).FirstName;
                lb_OrderDate.Text = DateTime.Now.ToString("dd/MM/yyyy");

                if (type == "1")
                {
                    lb_name.Text = ((TblUserAccount)Session[Global.SESSION_LOGGED_USER]).FirstName + " " + ((TblUserAccount)Session[Global.SESSION_LOGGED_USER]).LastName;
                    lb_phone.Text = ((TblUserAccount)Session[Global.SESSION_LOGGED_USER]).Phone;
                    lb_address.Text = ((TblUserAccount)Session[Global.SESSION_LOGGED_USER]).Address;

                }
                else if (type == "2")
                {
                    lb_address.Text = ((TblShippTo)Session[Global.SESSION_SHIPTO1]).CusAddress;
                    lb_name.Text = ((TblShippTo)Session[Global.SESSION_SHIPTO1]).FName + " " + ((TblShippTo)Session[Global.SESSION_SHIPTO1]).LName;
                    lb_phone.Text = ((TblShippTo)Session[Global.SESSION_SHIPTO1]).MobilePhone;

                }
                List<TblBookingDetail> arrItems = new List<TblBookingDetail>();
                if (Session[Global.SESSION_CART] != null)
                    arrItems = (List<TblBookingDetail>)Session[Global.SESSION_CART];
                grdShoppingCart.DataSource = arrItems;
                grdShoppingCart.DataBind();

                lblTotal.Text = (float.Parse(Session[Global.SESSION_TOTAL].ToString())).ToString("0,00.##");
            }
        }
    }
    protected void btnthanhtoan_Click(object sender, EventArgs e)
    {
        if (Session[Global.SESSION_LOGGED_USER] == null
                 || Session[Global.SESSION_CART] == null)
        {
            Response.Redirect("~/Default.aspx");
        }
        else
        {
            bool flag = false;
            try
            {
                bool status;
                string method = Session["method"].ToString();

                if (Session["type"].ToString() == "1")
                {
                    TblShippTo shipto = (TblShippTo)Session[Global.SESSION_SHIPTO];
                    TblShippToDAO shiptoDAO = new TblShippToDAO(Global.CONNECTION_STRING);
                    status = shiptoDAO.Update(shipto);
                }
                else
                {
                    TblShippTo shipto1 = (TblShippTo)Session[Global.SESSION_SHIPTO1];
                    TblShippToDAO shiptoDAO = new TblShippToDAO(Global.CONNECTION_STRING);
                    status = shiptoDAO.Update(shipto1);
                }

                TblBookingDAO orderdao = new TblBookingDAO(Global.CONNECTION_STRING);
                TblBooking order = new TblBooking();
                TblUserAccount userAccount = (TblUserAccount)Session[Global.SESSION_LOGGED_USER];
                order.Customer = userAccount;
                order.CusID = userAccount.AccountID;

                if (method == "1")
                {
                    order.PaymentMethod = 1;
                }
                else
                {
                    order.PaymentMethod = 2;

                }
                //order.DateBook = Convert.ToDateTime(DateTime.Now.ToString("dd/MM/yyyy"));
                //order.DateActive = Convert.ToDateTime(DateTime.Now.ToString("dd/MM/yyyy"));
                order.DateBook = Convert.ToDateTime(DateTime.Now.ToString("MM/dd/yyyy"));
                order.DateActive = Convert.ToDateTime(DateTime.Now.ToString("MM/dd/yyyy"));
                order.TotalPrice = float.Parse(Session[Global.SESSION_TOTAL].ToString());

               
                List<TblBookingDetail> arrItems = (List<TblBookingDetail>)Session[Global.SESSION_CART];
                int id;
                orderdao.Insert(order, arrItems, out id);

                // update so luong san pham con lai                         
                int SLBanDau = 0; 
                int ValueOnHand = 0;
                int ItemID =0;

                foreach (TblBookingDetail detail in arrItems)
                {
                    ItemID = detail.ItemID;

                    TblItems dt = itemDAO.GetItemByID(ItemID);
                    SLBanDau = dt.ValueOnHand; 

                    ValueOnHand = detail.Quantity; // lay duoc so luong mua
                    int valueUp = SLBanDau - ValueOnHand;
                  
                    itemDAO.UpdateQuanlity(ItemID, valueUp);
                }

                order.BookID = id;

                Session[Global.SESSION_CART] = null;
                Session[Global.SESSION_SHIPTO] = null;
                Session[Global.SESSION_SHIPTO1] = null;
                Session["type"] = null;
                Session["method"] = null;

                flag = true;

            }
            catch (Exception ex)
            {
                Session[Global.SESSION_ERROR] = ex;
                Response.Redirect(Global.ERROR_PAGE, true);
            }
            if (flag)
            {
                Response.Redirect(string.Format("~/Default.aspx"));
            }
        }
    }
    protected void btnQuaylai_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/ShipTo.aspx");
    }
}
